import { _decorator, Component, find, instantiate, Node, Prefab } from 'cc';
import { MapCamera } from './MapCamera';
import { BundleManager, Bundles } from '../AshFramework/Managers/BundleManager';
import { GameManager } from './GameManager';
import { ZTool } from '../AshFramework/Utils/ZTool';
import { EnemyInitController } from './EnemyInitController';
import { PrefsManager } from '../AshFramework/Managers/PrefsManager';
import { WEAPON } from '../AshFramework/Datas/Constant';
import PlayerController from '../PlayerController';
import { TipsController } from './TipsController';
const { ccclass, property } = _decorator;

@ccclass('MapController')
export class MapController extends Component {
    public static Instance: MapController = null;

    @property(Node)
    InitPos: Node[] = [];

    @property
    MinX: number = 0;

    @property
    MaxX: number = 0;

    @property
    MinY: number = 0;

    @property
    MaxY: number = 0;

    @property
    IsMAP1: boolean = false;

    IsFinish: boolean = true;
    IsFinishAll: boolean = false;
    CurEnemyData: EnemyInitController = null;
    CreateName: string[] = [];


    protected onLoad(): void {
        MapController.Instance = this;
    }

    protected start(): void {
        MapCamera.Instance.setClamp(this.MinX, this.MaxX, this.MinY, this.MaxY);
        if (this.node.name === "MAP1" && PrefsManager.Instance.userData.HaveWeapon.findIndex(e => e == WEAPON.自动手枪) != -1) {
            find("Prop/枪", this.node).active = false;
        }

        if (this.IsMAP1 && TipsController.Instance.IsPack) {
            GameManager.Instance.PackButton.active = false;
        }
    }

    startCreateEnemy() {
        if (this.CreateName.length <= 0) {
            if (this.CurEnemyData.AllName.length <= 0) {
                this.createEnemy()
                return;
            }
            this.scheduleOnce(() => {
                this.createEnemy()
            }, this.CurEnemyData.DelayTime);
            return;
        }

        const enemyName = this.CreateName.shift();
        BundleManager.LoadPrefab(Bundles.Prefabs, enemyName).then((prefab: Prefab) => {
            const enemy: Node = instantiate(prefab);
            enemy.parent = GameManager.Instance.EnemyLayout;
            enemy.setWorldPosition(this.CurEnemyData.getPos());
            this.scheduleOnce(() => { this.startCreateEnemy() }, this.CurEnemyData.getDelayTime());
        })
    }

    createEnemy() {
        if (this.CurEnemyData.AllName.length <= 0) {
            this.IsFinish = true;
            return;
        }
        this.CreateName = this.CurEnemyData.AllName.shift();
        this.startCreateEnemy();
    }

    getEnemyData(data: EnemyInitController) {
        this.CurEnemyData = data;
        this.IsFinish = false
        this.createEnemy();
    }

    getPlayerInitPos() {
        return this.InitPos[ZTool.GetRandomInt(0, this.InitPos.length)].getWorldPosition();
    }
}


